<?php

/**
 * 统一文件上传
 *
 * @param string $path
 * @return Upload
 */
if (!function_exists('fish_upload_save')) {
    function fish_upload_save($path = "storage/uploads")
    {
        require_once __DIR__ . "/Upload.php";
        $upload = (new Upload);
        $field = 'file';
        if (!isset($_FILES[$field])) fish_fail("文件上传，请求有误！");
        $tmp = reset($_FILES);
        $tmp = reset($tmp);

        if (is_array($tmp)) {
            /*多文件上传*/
            for ($i = 0; $i < count($tmp); $i++) {
                $fileInfo = [];
                $fileInfo["name"] = $_FILES[$field]['name'][$i];
                $fileInfo["type"] = $_FILES[$field]['type'][$i];
                $fileInfo["tmp_name"] = $_FILES[$field]['tmp_name'][$i];
                $fileInfo["error"] = $_FILES[$field]['error'][$i];
                $fileInfo["size"] = $_FILES[$field]['size'][$i];
                $upload->uploadFiles($path, $fileInfo);
            }
        } else {
            /*单文件上传*/
            $upload->uploadFile($path);
            if ($upload->errorNumber) fish_fail($upload->errorMessage, [], $upload->errorNumber);
        }
        return $upload;
    }
}